package com.example.royalchef.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.WindowManager;

import com.example.royalchef.utils.UIUtils;

/**
 * author: Administrator
 * created on: 2017/6/30 17:09
 * description:
 */

public class FitImageView extends android.support.v7.widget.AppCompatImageView {
    private int imageWidth;
    public FitImageView(Context context) {
        super(context);
        init(context);
    }

    public FitImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int margin = UIUtils.dip2px(10);
        imageWidth = (width - margin * 5) / 4;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(imageWidth, imageWidth);
    }
}
